﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class Category : System.Web.UI.UserControl
    {
        public int count = 1;
        public static int idCat = 0;
        DataTable dt;
        public static List<LIB.Sort> mysort = new List<LIB.Sort>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                mysort.Clear();
                ListItem lt = new ListItem("Chọn danh mục", "-1");
                dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT * FROM tbl_CategoryTop ORDER BY CategoryTop_Ordinal ASC", true);
                LIB.Sort s;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s = new LIB.Sort(dt.Rows[i]["CategoryTop_ID1"].ToString(), dt.Rows[i]["CategoryTop_Ordinal"].ToString());
                    mysort.Add(s);
                }
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }
            
        }
        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void rpt_Sanpham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "imgup")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());

                int i = 0;
                for (i = 0; i < mysort.Count; i++)
                {
                    if (mysort[i].id == id.ToString())
                    {
                        break;
                    }
                }


                if (i > 0)
                {
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_CategoryTop SET CategoryTop_Ordinal=" + mysort[i - 1].index + " Where CategoryTop_ID1=" + mysort[i].id, false);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_CategoryTop SET CategoryTop_Ordinal=" + mysort[i].index + " Where CategoryTop_ID1=" + mysort[i - 1].id, false);
                    Response.Redirect("/Index.aspx?panel=2&sub=1");
                }
            }

            if (e.CommandName == "imgdown")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());

                int i = 0;
                for (i = 0; i < mysort.Count; i++)
                {
                    if (mysort[i].id == id.ToString())
                    {
                        break;
                    }
                }

                if (i < mysort.Count - 1)
                {
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_CategoryTop SET CategoryTop_Ordinal=" + mysort[i + 1].index + " Where CategoryTop_ID1=" + mysort[i].id, false);
                    DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Update tbl_CategoryTop SET CategoryTop_Ordinal=" + mysort[i].index + " Where CategoryTop_ID1=" + mysort[i + 1].id, false);
                    Response.Redirect("/Index.aspx?panel=2&sub=1");
                }
            }

            if (e.CommandName == "delete")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_CategoryTop", "CategoryTop_ID1", id.ToString());
                Response.Redirect("/Index.aspx?panel=2&sub=1");
            }
        }

        protected void lbtRoot_Click(object sender, EventArgs e)
        {
        }
    }
}